package com.example.dml.proxy_pattern.dynamic_proxy;

import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * @Authors：gaoyang
 * @CreateTime：2021-10-27
 * @Description：todo
 */
public class JDKMeipo implements InvocationHandler {

    private Object target;

    public Object getInstance(Object target){
        this.target = target;
        Class<?> clazz = target.getClass();
        return Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
    }
    @Override
    public Object invoke(Object o, Method method, Object[] args) throws Throwable {
        before();
        System.out.println("调用的方法为："+method.getName());
        Object obj = method.invoke(this.target,args);
        after();
        return obj;
    }

    private void before(){
        System.out.println("我是媒婆。我要给你介绍对象，现在已经确认你的需求，开始物色");
    }

    private void after(){
        System.out.println("如果合适的话，就准备办事");
    }
}
